import apiService from "@service/index";

export const channelApiService = apiService
.enhanceEndpoints({ addTagTypes: ["userChannels"] })
.injectEndpoints({
  endpoints: (builder) => ({
     // 获取用户频道列表
    requestUserChannels: builder.query<ChannelResponse, undefined>({
      query: () => ({ url: "/user/channels" }),
      providesTags: ["userChannels"],
    })
  }),
})
.injectEndpoints({
  endpoints: (builder) => ({
    // 获取访客频道列表
    // 由于访客频道列表不需要被自动缓存到 API Reducer 函数中
    // 所以此处使用 mutation 请求, mutation 请求不会缓存响应状态
    requestGuestChannels: builder.mutation<ChannelResponse, undefined>({
      query: () => ({ url: "/user/channels" }),
    }),
  }),
})
.injectEndpoints({
  endpoints: (builder) => ({
    // 删除用户频道
    deleteUserChannel: builder.mutation<null, number>({
      query: (id) => ({ url: `/user/channels/${id}`, method: "DELETE" }),
      invalidatesTags: ["userChannels"],
    }),
  }),
})
.injectEndpoints({
  endpoints: (builder) => ({
    // 获取所有频道列表
    requestAllChannels: builder.query<ChannelResponse, undefined>({
      query: () => ({ url: "/channels" }),
    }),
  }),
})
.injectEndpoints({
  endpoints: (builder) => ({
    // 添加用户频道
    addUserChannel: builder.mutation<AddChannelResponse, { channels: ChannelState[] }>({
      query: (body) => ({ url: "/user/channels", method: "PATCH", body }),
      invalidatesTags: ["userChannels"],
    }),
  }),
});

export const {useRequestUserChannelsQuery,useDeleteUserChannelMutation,useRequestGuestChannelsMutation ,useRequestAllChannelsQuery,useAddUserChannelMutation} = channelApiService;